package com.relax.mybatis.encrypt.config;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import com.relax.mybatis.annotation.EncryptField;
import com.relax.mybatis.constant.Algorithm;
import com.relax.mybatis.constant.EncryptConstants;

import lombok.Data;

/**
 * @author hebin
 * @date 2022/07/05
 * @desc 加密配置
 */
@Data
@ConfigurationProperties(prefix = "mybatis.plugins.encrypt")
public class EncryptProperties implements Serializable {

    private static final long serialVersionUID = 6405782768252574254L;

    /**
     * 加密密码
     */
    private String password = EncryptConstants.DEFAULT_PASSWORD;

    /**
     * 公钥
     */
    private String publicKey = EncryptConstants.DEFAULT_PUBLIC_KEY;

    /**
     * 私钥
     */
    private String privateKey = EncryptConstants.DEFAULT_PRIVATE_KEY;

    /**
     * 加密算法
     */
    private Algorithm algorithm;

    /**
     * 字符串输出类型，默认 BASE64
     */
    private String stringOutputType = EncryptConstants.STRING_OUTPUT_TYPE_BASE64;

    public String getPassword(EncryptField encryptField) {
        String password = encryptField.password();
        return (null != this.password && EncryptConstants.DEFAULT_PASSWORD.equals(password)) ? this.password : password;
    }

    public Algorithm getAlgorithm(EncryptField encryptField) {
        Algorithm algorithm = encryptField.algorithm();
        return (null != this.algorithm && algorithm == Algorithm.PBEWithMD5AndDES) ? this.algorithm : algorithm;
    }

}
